import time

import pyautogui
from anyio import sleep

from utils import TimerTask,ToNumber
from cnocr import CnOcr
from pynput import keyboard
import  asyncio

wh = pyautogui.size()
#赢的分数，都设置大于X为赢
Target = 50
#赢的次数
WinNum = 0
#输的次数
DonateNum = 0
#总次数
Totals = 0

#图片识别
def ocrImg(path):
    oc = CnOcr()
    out = oc.ocr(path)
    return out

flag = False

print('开始执行')
#定时器
def create():
    global WinNum, DonateNum,Totals
    if flag:
        lastImgList = ocrImg('fo1o.png')
        lastCountRes = ToNumber(lastImgList[0]['text'])
        
        pyautogui.leftClick(346, 558, )
        pyautogui.screenshot('fo1o.png', region=(1740, 240, 90, 50))
        imgList = ocrImg('fo1o.png')
        if not imgList:
            return
        # 掷出来的结果
        countRes = ToNumber(imgList[0]['text'])
        if countRes>Target:
            #赢的情况
            WinNum= WinNum+1
        else:
            #输的情况
            DonateNum = DonateNum+1
        #计算总次数
        Totals = WinNum+DonateNum
        #计算进场概率
        winChance = float(WinNum / Totals)
        print('数字'+str(countRes)+'进场概率:' + str(winChance)+'----'+str(WinNum)+'/'+str(DonateNum)+'----'+str(Totals))

        if winChance < 0.5:
            print('进场概率:' + str(winChance))
            # todo 输入数字
        else:
            print('未达到要求')


TimerTask(2,create).start()


def on_press(key):
    print(key)
    global flag
    if key == keyboard.Key.delete :
        flag = False
    if key == keyboard.Key.home:
        flag = True


with keyboard.Listener(on_press=on_press) as listener:
    listener.join()





